package eudjok
import java.util.*;
import java.text.*;


class MusicaController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [musicaInstanceList: Musica.list(params), musicaInstanceTotal: Musica.count()]
    }
	
	def top ={
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy")
		Date data = new Date()
		String dateResult = dateFormat.format (data)
	   
		//Sepera a data
		String [] separa_data = dateResult.split("/")
		
				
		def resultDia = AvaliacaoConsolidada.createCriteria().list(max:5, sort:'avaliacaoPositiva', order:'desc') {
			eq('tipo','Musica')  
			eq('dia',separa_data[0])
			eq ('mes',separa_data[1])
		    eq ('ano',separa_data[2])
			
		}
		
		def resultIndicadas = AvaliacaoConsolidada.createCriteria().list(max:5, sort:'indicacoes', order:'desc') { 
			eq('tipo','Musica')
			
		}
		
		def resultAcessadas = AvaliacaoConsolidada.createCriteria().list(max:5, sort:'visitas', order:'desc') {
			eq('tipo','Musica')
		}
		
		def resultAvaliadas = AvaliacaoConsolidada.createCriteria().list(max:5, sort:'avaliacaoPositiva', order:'desc') {
			eq('tipo','Musica')
		}
		
		def resultMensal = AvaliacaoConsolidada.createCriteria().list(max:5, sort:'avaliacaoPositiva', order:'desc') {
			eq('tipo','Musica')
			eq ('mes',separa_data[1])
		}
		
		def resultAnual = AvaliacaoConsolidada.createCriteria().list(max:5, sort:'avaliacaoPositiva', order:'desc') {
			eq('tipo','Musica')
			eq('ano',separa_data[2])
		}	
		
		def musica = 
		
        [musicaInstanceList: Musica.list(params), musicaInstanceTotal: Musica.count(), musicaInstanceListDia: resultDia,musicaInstanceTotal: Musica.count(),
		 musicaInstanceListIndicadas: resultIndicadas, musicaInstanceTotal: Musica.count(), musicaInstanceListAcessadas: resultAcessadas,
		 musicaInstanceTotal: Musica.count(), musicaInstanceListAvaliadas: resultAvaliadas, musicaInstanceTotal: Musica.count(),
		 musicaInstanceListMensal: resultMensal, musicaInstanceTotal: Musica.count(), musicaInstanceListAnual: resultAnual, musicaInstanceTotal: Musica.count()]
	
	}
	
    def create = {
        def musicaInstance = new Musica()
        musicaInstance.properties = params
        return [musicaInstance: musicaInstance]
    }

    def save = {
        def musicaInstance = new Musica(params)
       
        musicaInstance.dataCriacao = new Date()
        musicaInstance.ultimaAtualizacao = new Date()
        musicaInstance.dj = session.dj
		
        if (musicaInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'musica.label', default: 'Musica'), musicaInstance.id])}"
            redirect(action: "show", id: musicaInstance.id)
        }
        else {
            render(view: "create", model: [musicaInstance: musicaInstance])
        }
    }

	 def saveYoutube = {
	 	println(params)
        def musicaInstance = new Musica(params)
        
        def estilo = Estilo.findById(1)
       
		musicaInstance.estilo = estilo       
        musicaInstance.dataCriacao = new Date()
        musicaInstance.ultimaAtualizacao = new Date()
        musicaInstance.dj = session.dj

        if (musicaInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'musica.label', default: 'Musica'), musicaInstance.id])}"
            redirect(controller:"home", action: "index", id: musicaInstance.id)
        }
        else {
            render(view: "create", model: [musicaInstance: musicaInstance])
        }
    }
	
    def show = {
        def musicaInstance = Musica.get(params.id)
        if (!musicaInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'musica.label', default: 'Musica'), params.id])}"
            redirect(action: "list")
        }
        else {
			musicaInstance.visitas = musicaInstance.visitas + 1
			musicaInstance.save(flush: true)
            [musicaInstance: musicaInstance]
        }
    }

    def edit = {
        def musicaInstance = Musica.get(params.id)
        if (!musicaInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'musica.label', default: 'Musica'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [musicaInstance: musicaInstance]
        }
    }

    def update = {
        def musicaInstance = Musica.get(params.id)
        musicaInstance.ultimaAtualizacao = new Date()
        if (musicaInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (musicaInstance.version > version) {
                    
                    musicaInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'musica.label', default: 'Musica')] as Object[], "Another user has updated this Musica while you were editing")
                     render(view: "edit", model: [musicaInstance: musicaInstance])
                    return
                }
            }
            musicaInstance.properties = params
            if (!musicaInstance.hasErrors() && musicaInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'musica.label', default: 'Musica'), musicaInstance.id])}"
                redirect(action: "show", id: musicaInstance.id)
            }
            else {
                render(view: "edit", model: [musicaInstance: musicaInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'musica.label', default: 'Musica'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def musicaInstance = Musica.get(params.id)
        if (musicaInstance) {
            try {
                musicaInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'musica.label', default: 'Musica'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'musica.label', default: 'Musica'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'musica.label', default: 'Musica'), params.id])}"
            redirect(action: "list")
        }
    }
 }